﻿using Mjollnir.Live;
using Mjollnir.Live.Controls;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;

namespace MeConsoleApplication1
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            var session = default(LiveConnectSession);

            Console.WriteLine("Sign-In with LiveAuthDialog.ShowDialog()");
            {
                using (var dialog = new SignInDialog())
                {
                    // Update the ClientID with your app client Id that you created from https://account.live.com/developers/applications.
                    //dialog.ClientId = "%YourAppClientId%";
                    dialog.ClientId = File.ReadAllLines(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"..\Dropbox\Documents\Keys\PseudoLiveSDK_ClientID.txt")).First().Trim();
                    //dialog.Locale = "ja";
                    dialog.Scopes = new[] { "wl.signin" };

                    dialog.ShowInTaskbar = true;

                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        session = dialog.Session;
                    }
                    else
                    {
                        if (dialog.Error == null)
                        {
                            Console.WriteLine("Cancelled.");
                        }
                        else
                        {
                            Console.WriteLine(dialog.Error.Message);
                        }
                        Console.WriteLine();

                        Console.WriteLine("Hit enter key.");
                        Console.ReadLine();
                        return;
                    }
                }
            }
            Console.WriteLine();

            Console.WriteLine("Getting account informations using LiveConnectClient.Get()");
            {
                var eventArgs = default(LiveOperationCompltedEventArgs);

                using (var signal = new ManualResetEvent(false))
                {
                    var client = new LiveConnectClient(session);

                    client.GetCompleted += (s, e) => { eventArgs = e; signal.Set(); };
                    client.GetAsync("me", signal);

                    signal.WaitOne();
                }

                if (eventArgs.Error == null && !eventArgs.Cancelled)
                {
                    foreach (var key in eventArgs.Result.Keys)
                    {
                        Console.WriteLine(string.Format("{0}={1}", key, eventArgs.Result[key]));
                    }
                }
                else
                {
                    if (eventArgs.Cancelled)
                    {
                        Console.WriteLine("Cancelled.");
                    }
                    else
                    {
                        Console.WriteLine(eventArgs.Error.Message);
                    }
                }
            }
            Console.WriteLine();

            Console.WriteLine("Hit enter key.");
            Console.ReadLine();
        }
    }
}
